package com.smart.community.region.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smart.community.feign.region.dto.BuildingInfoDTO;
import com.smart.community.feign.region.dto.CommunityInfoDTO;
import com.smart.community.feign.region.dto.HouseholdInfoDTO;
import com.smart.community.feign.region.dto.RegionDTO;
import com.smart.community.feign.region.dto.UnitInfoDTO;
import com.smart.community.region.service.IBuildingService;
import com.smart.community.region.service.ICommunityService;
import com.smart.community.region.service.IHouseholdService;
import com.smart.community.region.service.IRegionService;
import com.smart.community.region.service.IUnitService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

/**
 * 区域数据Feign接口实现Controller
 * 提供房户、楼栋、单元、社区等基础数据查询接口，不进行权限验证
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 * @version 2.0.0 - 严格按照Controller规范和Api规范重构
 */
@Slf4j
@RestController
@Validated
@RequestMapping("/region/data")
@Tag(name = "区域数据Feign接口", description = "提供房户、楼栋、单元、社区等基础数据查询接口")
public class RegionDataFeignController {

    @Autowired
    private IHouseholdService householdService;

    @Autowired
    private IBuildingService buildingService;

    @Autowired
    private IUnitService unitService;

    @Autowired
    private ICommunityService communityService;

    @Autowired
    private IRegionService regionService;

    /**
     * 根据房户ID获取房户信息
     * 
     * @param householdId 房户ID
     * @return 房户信息
     */
    @GetMapping("/feign/region/household/{householdId}")
    @Operation(summary = "根据房户ID获取房户信息", description = "Feign接口：根据房户ID获取房户信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = HouseholdInfoDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public HouseholdInfoDTO getHouseholdById(
            @Parameter(description = "房户ID", required = true)
            @PathVariable("householdId") @NotNull @Positive Long householdId) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据房户ID获取房户信息开始，householdId: {}", householdId);
        
        try {
            // TODO: 待IHouseholdService实现getHouseholdByIdForFeign方法
            log.warn("Feign接口：IHouseholdService.getHouseholdByIdForFeign方法尚未实现");
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据房户ID获取房户信息完成，householdId: {}，响应时间: {}ms", householdId, responseTime);
            
            return null;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据房户ID获取房户信息异常，householdId: {}，响应时间: {}ms", householdId, responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("获取房户信息失败", e);
        }
    }

    /**
     * 根据楼栋ID获取楼栋信息
     * 
     * @param buildingId 楼栋ID
     * @return 楼栋信息
     */
    @GetMapping("/feign/region/building/{buildingId}")
    @Operation(summary = "根据楼栋ID获取楼栋信息", description = "Feign接口：根据楼栋ID获取楼栋信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = BuildingInfoDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public BuildingInfoDTO getBuildingById(
            @Parameter(description = "楼栋ID", required = true)
            @PathVariable("buildingId") @NotNull @Positive Long buildingId) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据楼栋ID获取楼栋信息开始，buildingId: {}", buildingId);
        
        try {
            // TODO: 待IBuildingService实现getBuildingByIdForFeign方法
            log.warn("Feign接口：IBuildingService.getBuildingByIdForFeign方法尚未实现");
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据楼栋ID获取楼栋信息完成，buildingId: {}，响应时间: {}ms", buildingId, responseTime);
            
            return null;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据楼栋ID获取楼栋信息异常，buildingId: {}，响应时间: {}ms", buildingId, responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("获取楼栋信息失败", e);
        }
    }

    /**
     * 根据单元ID获取单元信息
     * 
     * @param unitId 单元ID
     * @return 单元信息
     */
    @GetMapping("/feign/region/unit/{unitId}")
    @Operation(summary = "根据单元ID获取单元信息", description = "Feign接口：根据单元ID获取单元信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = UnitInfoDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public UnitInfoDTO getUnitById(
            @Parameter(description = "单元ID", required = true)
            @PathVariable("unitId") @NotNull @Positive Long unitId) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据单元ID获取单元信息开始，unitId: {}", unitId);
        
        try {
            // 调用Service层方法获取单元信息
            UnitInfoDTO result = unitService.getUnitByIdForFeign(unitId);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据单元ID获取单元信息完成，unitId: {}，响应时间: {}ms", unitId, responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据单元ID获取单元信息异常，unitId: {}，响应时间: {}ms", unitId, responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("获取单元信息失败", e);
        }
    }

    /**
     * 根据社区ID获取社区信息
     * 
     * @param communityId 社区ID
     * @return 社区信息
     */
    @GetMapping("/feign/region/community/{communityId}")
    @Operation(summary = "根据社区ID获取社区信息", description = "Feign接口：根据社区ID获取社区信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = CommunityInfoDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public CommunityInfoDTO getCommunityById(
            @Parameter(description = "社区ID", required = true)
            @PathVariable("communityId") @NotNull @Positive Long communityId) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据社区ID获取社区信息开始，communityId: {}", communityId);
        
        try {
            CommunityInfoDTO result = communityService.getCommunityByIdForFeign(communityId);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据社区ID获取社区信息完成，communityId: {}，响应时间: {}ms", communityId, responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据社区ID获取社区信息异常，communityId: {}，响应时间: {}ms", communityId, responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("获取社区信息失败", e);
        }
    }

    /**
     * 批量获取房户信息
     * 
     * @param householdIds 房户ID列表
     * @return 房户信息列表
     */
    @PostMapping("/feign/region/households/batch")
    @Operation(summary = "批量获取房户信息", description = "Feign接口：批量获取房户信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = HouseholdInfoDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<HouseholdInfoDTO> getHouseholdsByIds(
            @Parameter(description = "房户ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> householdIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：批量获取房户信息开始，householdIds数量: {}", householdIds.size());
        
        try {
            List<HouseholdInfoDTO> result = householdService.getHouseholdsByIdsForFeign(householdIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：批量获取房户信息完成，householdIds数量: {}，房户信息数量: {}，响应时间: {}ms", 
                householdIds.size(), result.size(), responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：批量获取房户信息异常，householdIds数量: {}，响应时间: {}ms", householdIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("批量获取房户信息失败", e);
        }
    }

    /**
     * 批量获取楼栋信息
     * 
     * @param buildingIds 楼栋ID列表
     * @return 楼栋信息列表
     */
    @PostMapping("/feign/region/buildings/batch")
    @Operation(summary = "批量获取楼栋信息", description = "Feign接口：批量获取楼栋信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = BuildingInfoDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<BuildingInfoDTO> getBuildingsByIds(
            @Parameter(description = "楼栋ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> buildingIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：批量获取楼栋信息开始，buildingIds数量: {}", buildingIds.size());
        
        try {
            // TODO: 待IBuildingService实现getBuildingsByIdsForFeign方法
            log.warn("Feign接口：IBuildingService.getBuildingsByIdsForFeign方法尚未实现");
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：批量获取楼栋信息完成，buildingIds数量: {}，响应时间: {}ms", buildingIds.size(), responseTime);
            
            return Collections.emptyList();
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：批量获取楼栋信息异常，buildingIds数量: {}，响应时间: {}ms", buildingIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("批量获取楼栋信息失败", e);
        }
    }

    /**
     * 批量获取单元信息
     * 
     * @param unitIds 单元ID列表
     * @return 单元信息列表
     */
    @PostMapping("/feign/region/units/batch")
    @Operation(summary = "批量获取单元信息", description = "Feign接口：批量获取单元信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = UnitInfoDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<UnitInfoDTO> getUnitsByIds(
            @Parameter(description = "单元ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> unitIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：批量获取单元信息开始，unitIds数量: {}", unitIds.size());
        
        try {
            // 调用Service层方法批量获取单元信息
            List<UnitInfoDTO> result = unitService.getUnitsByIdsForFeign(unitIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：批量获取单元信息完成，unitIds数量: {}，返回数量: {}，响应时间: {}ms", 
                    unitIds.size(), result != null ? result.size() : 0, responseTime);
            
            return result != null ? result : Collections.emptyList();
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：批量获取单元信息异常，unitIds数量: {}，响应时间: {}ms", unitIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("批量获取单元信息失败", e);
        }
    }

    /**
     * 批量获取社区信息
     * 
     * @param communityIds 社区ID列表
     * @return 社区信息列表
     */
    @PostMapping("/feign/region/communities/batch")
    @Operation(summary = "批量获取社区信息", description = "Feign接口：批量获取社区信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = CommunityInfoDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<CommunityInfoDTO> getCommunitiesByIds(
            @Parameter(description = "社区ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> communityIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：批量获取社区信息开始，communityIds数量: {}", communityIds.size());
        
        try {
            List<CommunityInfoDTO> result = communityService.getCommunitiesByIdsForFeign(communityIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：批量获取社区信息完成，communityIds数量: {}，响应时间: {}ms", communityIds.size(), responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：批量获取社区信息异常，communityIds数量: {}，响应时间: {}ms", communityIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("批量获取社区信息失败", e);
        }
    }
    
    /**
     * 根据社区ID获取房户ID集合
     * 用于业主统计功能，获取指定社区下的所有房户ID
     * 
     * @param communityIds 社区ID列表
     * @return 房户ID列表
     */
    @PostMapping("/feign/region/household-ids/by-communities")
    @Operation(summary = "根据社区ID获取房户ID集合", description = "Feign接口：根据社区ID获取房户ID集合，用于业主统计功能")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = Long.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<Long> getHouseholdIdsByCommunityIds(
            @Parameter(description = "社区ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> communityIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据社区ID获取房户ID集合开始，communityIds数量: {}", communityIds.size());
        
        try {
            List<Long> result = householdService.getHouseholdIdsByCommunityIds(communityIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据社区ID获取房户ID集合完成，communityIds数量: {}，房户ID数量: {}，响应时间: {}ms", 
                communityIds.size(), result.size(), responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据社区ID获取房户ID集合异常，communityIds数量: {}，响应时间: {}ms", communityIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("根据社区ID获取房户ID集合失败", e);
        }
    }

    /**
     * 根据社区ID获取楼栋ID集合
     * 用于物业公司统计功能，获取指定社区下的所有楼栋ID
     * 
     * @param communityIds 社区ID列表
     * @return 楼栋ID列表
     */
    @PostMapping("/feign/region/building-ids/by-communities")
    @Operation(summary = "根据社区ID获取楼栋ID集合", description = "Feign接口：根据社区ID获取楼栋ID集合，用于物业公司统计功能")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = Long.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<Long> getBuildingIdsByCommunityIds(
            @Parameter(description = "社区ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> communityIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据社区ID获取楼栋ID集合开始，communityIds数量: {}", communityIds.size());
        
        try {
            List<Long> result = buildingService.getBuildingIdsByCommunityIds(communityIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据社区ID获取楼栋ID集合完成，communityIds数量: {}，楼栋ID数量: {}，响应时间: {}ms", 
                communityIds.size(), result.size(), responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据社区ID获取楼栋ID集合异常，communityIds数量: {}，响应时间: {}ms", communityIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("根据社区ID获取楼栋ID集合失败", e);
        }
    }

    /**
     * 根据楼栋ID获取单元ID集合
     * 用于物业公司统计功能，获取指定楼栋下的所有单元ID
     * 
     * @param buildingIds 楼栋ID列表
     * @return 单元ID列表
     */
    @PostMapping("/feign/region/unit-ids/by-buildings")
    @Operation(summary = "根据楼栋ID获取单元ID集合", description = "Feign接口：根据楼栋ID获取单元ID集合，用于物业公司统计功能")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = Long.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<Long> getUnitIdsByBuildingIds(
            @Parameter(description = "楼栋ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> buildingIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据楼栋ID获取单元ID集合开始，buildingIds数量: {}", buildingIds.size());
        
        try {
            List<Long> result = unitService.getUnitIdsByBuildingIds(buildingIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据楼栋ID获取单元ID集合完成，buildingIds数量: {}，单元ID数量: {}，响应时间: {}ms", 
                buildingIds.size(), result.size(), responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据楼栋ID获取单元ID集合异常，buildingIds数量: {}，响应时间: {}ms", buildingIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("根据楼栋ID获取单元ID集合失败", e);
        }
    }

    /**
     * 根据单元ID获取房户ID集合
     * 用于物业公司统计功能，获取指定单元下的所有房户ID
     * 
     * @param unitIds 单元ID列表
     * @return 房户ID列表
     */
    @PostMapping("/feign/region/household-ids/by-units")
    @Operation(summary = "根据单元ID获取房户ID集合", description = "Feign接口：根据单元ID获取房户ID集合，用于物业公司统计功能")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = Long.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<Long> getHouseholdIdsByUnitIds(
            @Parameter(description = "单元ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> unitIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据单元ID获取房户ID集合开始，unitIds数量: {}", unitIds.size());
        
        try {
            List<Long> result = householdService.getHouseholdIdsByUnitIds(unitIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据单元ID获取房户ID集合完成，unitIds数量: {}，房户ID数量: {}，响应时间: {}ms", 
                unitIds.size(), result.size(), responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据单元ID获取房户ID集合异常，unitIds数量: {}，响应时间: {}ms", unitIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("根据单元ID获取房户ID集合失败", e);
        }
    }

    /**
     * 根据社区ID获取完整的层级结构ID集合
     * 用于物业公司统计功能，一次性获取社区→楼栋→单元→房户的完整层级ID
     * 
     * @param communityIds 社区ID列表
     * @return 包含各层级ID的Map结构
     */
    @PostMapping("/feign/region/hierarchy-ids/by-communities")
    @Operation(summary = "根据社区ID获取完整层级结构ID集合", description = "Feign接口：根据社区ID获取完整层级结构ID集合，用于物业公司统计功能")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = Map.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Map<String, List<Long>> getHierarchyIdsByCommunityIds(
            @Parameter(description = "社区ID列表", required = true)
            @RequestBody @NotEmpty List<@NotNull @Positive Long> communityIds) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据社区ID获取完整层级结构ID集合开始，communityIds数量: {}", communityIds.size());
        
        try {
            // 获取楼栋ID
            List<Long> buildingIds = buildingService.getBuildingIdsByCommunityIds(communityIds);
            
            // 获取单元ID
            List<Long> unitIds = unitService.getUnitIdsByBuildingIds(buildingIds);
            
            // 获取房户ID
            List<Long> householdIds = householdService.getHouseholdIdsByUnitIds(unitIds);
            
            // 组装结果
            Map<String, List<Long>> hierarchyIds = new HashMap<>();
            hierarchyIds.put("communityIds", communityIds);
            hierarchyIds.put("buildingIds", buildingIds);
            hierarchyIds.put("unitIds", unitIds);
            hierarchyIds.put("householdIds", householdIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：获取完整层级结构ID集合完成，社区数量：{}，楼栋数量：{}，单元数量：{}，房户数量：{}，响应时间: {}ms", 
                communityIds.size(), buildingIds.size(), unitIds.size(), householdIds.size(), responseTime);
            
            return hierarchyIds;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据社区ID获取完整层级结构ID集合异常，communityIds数量: {}，响应时间: {}ms", communityIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("根据社区ID获取完整层级结构ID集合失败", e);
        }
    }

    /**
     * 获取区域层级路径
     * 获取从根区域到指定区域的层级路径
     * 
     * @param regionId 区域ID
     * @return 区域层级路径列表
     */
    @GetMapping("/feign/region/{regionId}/path")
    @Operation(summary = "获取区域层级路径", description = "Feign接口：获取从根区域到指定区域的层级路径")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功", content = @Content(schema = @Schema(implementation = RegionDTO.class))),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public List<RegionDTO> getRegionPath(
            @Parameter(description = "区域ID", required = true)
            @PathVariable("regionId") @NotNull @Positive Long regionId) {
        
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：获取区域层级路径开始，regionId: {}", regionId);
        
        try {
            List<RegionDTO> result = regionService.getRegionPathForFeign(regionId);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：获取区域层级路径完成，regionId: {}，路径层级数: {}，响应时间: {}ms", 
                regionId, result != null ? result.size() : 0, responseTime);
            
            return result != null ? result : Collections.emptyList();
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：获取区域层级路径异常，regionId: {}，响应时间: {}ms", regionId, responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("获取区域层级路径失败", e);
        }
    }
    
    /**
     * 根据房户ID获取关联的用户ID列表
     * 用于报修工单查询，获取房户下所有业主和住户的用户ID
     * 
     * @param householdId 房户ID
     * @return 用户ID列表
     */
    @GetMapping("/feign/region/household/{householdId}/user-ids")
    public List<Long> getUserIdsByHouseholdId(@PathVariable("householdId") Long householdId) {
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据房户ID获取用户ID列表，householdId: {}", householdId);
        
        try {
            // 1. 获取业主用户ID列表
            List<Long> ownerUserIds = householdService.getOwnerUserIdsByHouseholdId(householdId);
            log.info("房户ID: {} 的业主用户ID数量: {}", householdId, ownerUserIds.size());
            
            // 2. 获取住户用户ID列表
            List<Long> residentUserIds = householdService.getResidentUserIdsByHouseholdId(householdId);
            log.info("房户ID: {} 的住户用户ID数量: {}", householdId, residentUserIds.size());
            
            // 3. 合并去重
            Set<Long> userIdSet = new HashSet<>();
            userIdSet.addAll(ownerUserIds);
            userIdSet.addAll(residentUserIds);
            
            List<Long> result = new ArrayList<>(userIdSet);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据房户ID获取用户ID列表完成，householdId: {}，用户ID数量: {}，响应时间: {}ms", 
                householdId, result.size(), responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据房户ID获取用户ID列表异常，householdId: {}，响应时间: {}ms", householdId, responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("根据房户ID获取用户ID列表失败", e);
        }
    }
    
    /**
     * 根据房户ID列表获取关联的用户ID列表
     * 用于报修工单查询，获取多个房户下所有业主和住户的用户ID
     * 
     * @param householdIds 房户ID列表
     * @return 用户ID列表
     */
    @PostMapping("/feign/region/households/user-ids")
    public List<Long> getUserIdsByHouseholdIds(@RequestBody List<Long> householdIds) {
        long startTime = System.currentTimeMillis();
        log.info("Feign接口：根据房户ID列表获取用户ID列表，householdIds数量: {}", householdIds.size());
        
        try {
            Set<Long> allUserIds = new HashSet<>();
            int totalOwnerUserIds = 0;
            int totalResidentUserIds = 0;
            
            for (Long householdId : householdIds) {
                log.debug("处理房户ID: {}", householdId);
                
                // 1. 获取业主用户ID列表
                List<Long> ownerUserIds = householdService.getOwnerUserIdsByHouseholdId(householdId);
                allUserIds.addAll(ownerUserIds);
                totalOwnerUserIds += ownerUserIds.size();
                log.debug("房户ID: {} 的业主用户ID数量: {}", householdId, ownerUserIds.size());
                
                // 2. 获取住户用户ID列表
                List<Long> residentUserIds = householdService.getResidentUserIdsByHouseholdId(householdId);
                allUserIds.addAll(residentUserIds);
                totalResidentUserIds += residentUserIds.size();
                log.debug("房户ID: {} 的住户用户ID数量: {}", householdId, residentUserIds.size());
            }
            
            List<Long> result = new ArrayList<>(allUserIds);
            
            long responseTime = System.currentTimeMillis() - startTime;
            log.info("Feign接口：根据房户ID列表获取用户ID列表完成，householdIds数量: {}，业主用户ID总数: {}，住户用户ID总数: {}，最终用户ID数量: {}，响应时间: {}ms", 
                householdIds.size(), totalOwnerUserIds, totalResidentUserIds, result.size(), responseTime);
            
            return result;
        } catch (Exception e) {
            long responseTime = System.currentTimeMillis() - startTime;
            log.error("Feign接口：根据房户ID列表获取用户ID列表异常，householdIds数量: {}，响应时间: {}ms", householdIds.size(), responseTime, e);
            // 对于Feign接口，异常应该向上抛出，让Feign客户端处理
            throw new RuntimeException("根据房户ID列表获取用户ID列表失败", e);
        }
    }
}
